package com.kaifamiao.N0805;

import java.util.Arrays;

public class IdentifyUtil {

    public static String[] KEYWORDS = {"abstract", "continue", "for", "new", "switch", "assert", "default", "if", "package", "synchronized",
            "boolean", "do", "goto", "private", "this", "break", "double", "implements", "protected", "throw",
            "byte", "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient",
            "catch", "extends", "int", "short", "try", "char", "final", "interface", "static", "void",
            "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while", "_"};
    static String[] EXCLUDE = {"true", "false", "null"};

    static {
        //对上面数组进行排序
        Arrays.sort(KEYWORDS);
        Arrays.sort(EXCLUDE);
    }

    public boolean isIdentify(String str) {
        if(str == null || str.isBlank()){
            return false;
        }
        //判断是否是关键字
        int index = Arrays.binarySearch(KEYWORDS,str);

        //不是关键字
        if(index <0){
            //是否是特殊值
            index = Arrays.binarySearch(EXCLUDE,str);
            if (index <0){

                //数字不能开头
                String first = str.substring(0,1);

                //第一位不是数字
                if(!first.matches("\\d")){
                    str = str.replace("$", "");
                    str = str.replace("￥", "");
                    str = str.replaceAll("\\w","");
                    if(str.length() == 0){
                        return true;
                    }
                }
            }
        }
        return false;
    }

}
